﻿/*
 * 由SharpDevelop创建。
 * 用户： Administrator
 * 日期: 2024/5/27
 * 时间: 1:28
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */
using System;
using System.IO;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Reflection;
using System.Linq;

namespace test
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		List<Type> types = new List<Type>();
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			HotKey.RegisterHotKey(Handle, 100, HotKey.KeyModifiers.Alt, Keys.W);//注册热键 Alt+1 老板键 我用着顺手
			 HotKey.RegisterHotKey(Handle, 200, HotKey.KeyModifiers.Alt, Keys.E); //显示
			 HotKey.RegisterHotKey(Handle, 300, HotKey.KeyModifiers.Alt, Keys.Q); //退出
			
			int width = Screen.PrimaryScreen.Bounds.Width;
			int height = Screen.PrimaryScreen.Bounds.Height;
			this.Location=new Point(width-this.Width,0);
			this.Height=height;
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
			String path = "dll";

			//第一种方法
			string[] files = Directory.GetFiles(path, "*.dll");
			
			foreach (string file in files)
			{
			   types.Clear();
                Assembly outerAsm = Assembly.LoadFrom(file);
                //反射出dll中的所有类型，加入到下拉框中
                types = outerAsm.GetTypes().ToList();
                types.Reverse();
                foreach (var item in types)
                { 
                	var form= (Activator.CreateInstance(item) as Form);//转换成窗体类
                	if (form != null)
	                {
//	                    TabPage tabPage = new TabPage();
//	                    tabPage.Text = form.Text;
//	                    tabControl1.TabPages.Add(tabPage);
//	                    form.TopLevel = false;
//	                    form.Dock = DockStyle.Fill;
//	                    form.Parent = tabPage;
//	                    form.Show();
	                    
	                    Panel pan=new Panel();
	                	pan.Height=form.Height;
	                	pan.Width=form.Width;
	                	pan.Parent=flowLayoutPanel1;
	                    form.TopLevel = false;
	                    form.Dock = DockStyle.Fill;
	                    pan.Controls.Add(form);
	                    form.Show();
	                }
                	
                }
			}
			//*******************************
		}
		void MainFormLeave(object sender, EventArgs e)
		{
			HotKey.UnregisterHotKey(Handle, 100);//注销Id号为100的热键设定
		}
		 
		//执行热键
        protected override void WndProc(ref Message m)
        {
            const int WM_HOTKEY = 0x0312;
            //按快捷键 
            switch (m.Msg)
            {
                case WM_HOTKEY:
                    switch (m.WParam.ToInt32())
                    {
                        case 100://按下的是Alt+1 
                            BossKey();//老板键
                            break;
                        case 200://按下的是Alt+1 
                            this.Visible = true;
			                this.WindowState = FormWindowState.Normal;
			                this.notifyIcon1.Visible = false;
                            break; 
                        case 300://按下的是Alt+1 
                            this.Close();
                            break; 
                         
                    }
                    break;
            }
            base.WndProc(ref m);
        }
        //老板键：显示|隐藏 窗体
        private void BossKey()
        {
            if (this.WindowState == FormWindowState.Normal)
            {
                this.WindowState = FormWindowState.Minimized;
                this.Hide();//隐藏窗体
                this.notifyIcon1.Visible = true; //使托盘图标可见
            }
            else
            {
                this.Visible = true;
                this.WindowState = FormWindowState.Normal;
                this.notifyIcon1.Visible = false;
            }
        }
 
		 
	}
}
